package nl.jjsoft.airlock.view.presenters
{
	import com.adobe.utils.StringUtil;
	
	import flash.desktop.Clipboard;
	import flash.desktop.ClipboardFormats;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.net.URLRequest;
	import flash.net.navigateToURL;
	
	import mx.controls.Alert;
	import mx.controls.CheckBox;
	import mx.core.Application;
	
	import nl.jjsoft.airlock.controller.ISafeController;
	import nl.jjsoft.airlock.domain.Model;
	import nl.jjsoft.airlock.utils.PasswordGenerator;
	import nl.jjsoft.airlock.view.AccountEditForm;
	import nl.jjsoft.airlock.view.presenters.helpers.AccountPresenterHelper;
	import nl.jjsoft.airlock.vo.AccountVo;
	import nl.jjsoft.airlock.vo.CredentialsVO;
	
	public class AccountEditFormPresenter extends AbstractPresenter
	{
		[Inject]
		public var model:Model;
		
		[Inject]
		public var safeController:ISafeController;
		
		[Inject]
		public var accountPresenterHelper:AccountPresenterHelper;
		
		private var accountDeleteConfirmDialog:Alert;
		
		// True if there are unsaved changes.
		private var dirty:Boolean = false;
		
		public function AccountEditFormPresenter()
		{
			super(AccountEditForm);
		}
		
		override protected function init():void {
			super.init();
		}
		
		/**
		* Since the AbstractViewMediator sets the view via Autowiring in Swiz,
		* we need to create a local getter to access the underlying, expected view
		* class type.
		*/
		public function get view():AccountEditForm {
			return this._view as AccountEditForm;
		}
		
		override protected function setViewListeners():void {
			registerEventHandler(view.toggleCharacters, MouseEvent.CLICK, togglePasswordCharacters, true);
			registerEventHandler(view.save, MouseEvent.CLICK, saveAccount);
			registerEventHandler(view.deleteBtn, MouseEvent.CLICK, deleteAccount);
			registerEventHandler(view.navigateUrl, MouseEvent.CLICK, navigateToUrl);	
			registerEventHandler(view.generate, MouseEvent.CLICK, generatePassword);	
			registerEventHandler(view.toClipBoard, MouseEvent.CLICK, copyPasswordToClipBoard);	
			
			registerEventHandler(view.nameInput, Event.CHANGE, valuesChanged);
			registerEventHandler(view.urlInput, Event.CHANGE, valuesChanged);
			registerEventHandler(view.usernameInput, Event.CHANGE, valuesChanged);
			registerEventHandler(view.passwordInput, Event.CHANGE, valuesChanged);
			registerEventHandler(view.descriptionInput, Event.CHANGE, valuesChanged);
		}
		
		public function togglePasswordCharacters(event:MouseEvent):void {
			var selected:Boolean = CheckBox(event.currentTarget).selected;
			view.passwordInput.displayAsPassword = !selected;
		}
		
		public function saveAccount():void {
			var account:AccountVo = this.readAccount();
			if (safeController.saveAccount(account) != -1) {
				model.selectedAccount.copyValues(account);
				dirty = false;
				view.warningValuesDirty.visible = false;
			}
		}
		
		public function deleteAccount():void {
			var account:AccountVo = model.selectedAccount;		
			accountPresenterHelper.deleteAccount(account);	
		}
		
		private function readAccount():AccountVo {
			var cred:CredentialsVO = new CredentialsVO();
			cred.user_name = StringUtil.trim(view.usernameInput.text);
			cred.user_pass = StringUtil.trim(view.passwordInput.text);
			var account:AccountVo = new AccountVo();
			account.id = model.selectedAccount.id;
			account.group_id = model.selectedAccount.group_id;					
			account.credentials = cred;
			account.name = StringUtil.trim(view.nameInput.text);
			account.notes = StringUtil.trim(view.descriptionInput.text);
			account.url = StringUtil.trim(view.urlInput.text);
			return account;	
		}
		
		[Mediate(event="AccountEvent.SHOW_EDIT_FORM")]
		public function showAccountEditForm():void {
			view.warningValuesDirty.visible = false;
			view.entryForm.visible = true;
			view.deleteBtn.visible = true;
			view.save.visible = true;
		}
		
		[Mediate(event="AccountEvent.HIDE_EDIT_FORM")]
		public function hideAccountEditForm():void {
			view.entryForm.visible = false;
			view.deleteBtn.visible = false;
			view.save.visible = false;	
		}
		
		[Mediate(event="AccountEvent.NEW_ACCOUNT")]
		public function newAccount():void {
			view.entryForm.visible = true;	
			view.deleteBtn.visible = true;
			view.save.visible = true;
			view.toggleCharacters.selected = false;
			view.passwordInput.displayAsPassword = true;
			view.warningValuesDirty.visible = false;
		}
		
		[Mediate(event="AccountEvent.SET_FOCUS_USERNAME")]
		public function setFocusUserName():void {
			view.usernameInput.setFocus();
		}
		
		private function navigateToUrl():void {
			navigateToURL(new URLRequest(view.urlInput.text));
		}
		
		private function generatePassword():void {
			view.passwordInput.text = PasswordGenerator.generatePassword(model.passwordPolicy);
			valuesChanged();
		}
		
		private function copyPasswordToClipBoard():void {
			var account:AccountVo = model.selectedAccount;
			if (account != null && account.credentials.user_pass != null) {
				Clipboard.generalClipboard.setData(ClipboardFormats.TEXT_FORMAT, account.credentials.user_pass);
			}
		}
		
		private function valuesChanged():void {
			dirty = true;
			view.warningValuesDirty.visible = true;
		}
	}
}